<?php

/**
 * 微信成员信息相关的操作
*/

class UserController extends BaseController {

    public function actionList() {
        if (Yii::app()->request->isAjaxRequest) {
            $page = Yii::app()->request->getPost('page', 1);
            $limit = Yii::app()->request->getPost('limit', 10);
            $name = Yii::app()->request->getPost('name',"");
            $page_data = User::model()->getPageList($page, $limit,$name);
            $this->jsonResponse($page_data);
        } else {
            $this->render('list');
        }
    }

    /**
     * 获取微信成员
    */
    public function actionGetList() {
        $model = User::model();
        $ret = $model->downloadUser();
        if ($ret === false) {
            $this->error($model->getErrors());
        }
        $this->success('微信成员获取成功');
    }

    /**
     * 用户图片权限
    */
    public function actionUserImg() {
        $userid = Yii::app()->request->getQuery('userid');
        if (empty($userid)) {
            $this->error('参数非法');
        }
        $user = User::model()->findByPk($userid);
        if (empty($user)) {
            $this->error('用户不存在');
        }
        $floder_list = Floder::model()->findAll('type=:type and pid>0', array(':type'=>'image'));
        $default_fid = isset($floder_list[0]) ? $floder_list[0]->id : 0; //默认相册
        $count = Image::model()->count("pid='{$default_fid}'");
        $params = array(
            'floder_list' => $floder_list,
            'default_floder' => $default_fid,
            'userid' => $userid,
            'username' => $user->name,
            'count' => intval($count)
        );
        $this->render('userImg', $params);
    }

    /**
     * 获取图片库的所有图片和用户的图片
    */
    public function actionGetUserImg() {
        if (!Yii::app()->request->isAjaxRequest) {
            return;
        }
        $floder_id = Yii::app()->request->getPost('pid');
        if (empty($floder_id)) {
            $this->error('相册未选择');
        }
        $type = Yii::app()->request->getParam('type');
        if ($type === 'count') {
            $count = Image::model()->count("pid='{$floder_id}'");
            $rep_data = array(
                'count' => intval($count),
                'code' => 0
            );
            $this->jsonResponse($rep_data);
        }
        $userid = Yii::app()->request->getPost('userid');
        if (empty($userid)) {
            $this->error('参数非法');
        }
        $user = User::model()->findByPk($userid);
        if (empty($user)) {
            $this->error('用户不存在');
        }

        $limit = intval(Yii::app()->request->getPost('limit', 10));
        $page = intval(Yii::app()->request->getPost('page', 1));

        $user_img = $user->getUserImg($page, $limit,$floder_id);
        $this->jsonResponse($user_img);
    }

    /**
     * 修改用户的图片权限
    */
    public function actionChangeImg() {
        if (!Yii::app()->request->isAjaxRequest) {
            return;
        }
        $userid = Yii::app()->request->getPost('userid');
        $img_id = Yii::app()->request->getPost('img_id');
        $pid = Yii::app()->request->getPost('pid');
        $is_checked = Yii::app()->request->getPost('is_checked');
        if (empty($userid) || empty($img_id) || empty($pid) || !in_array($is_checked, array('0','1'))) {
            $this->error('参数非法');
        }
        $user = User::model()->findByPk($userid);
        if ($user === null) {
            $this->error('用户不存在');
        }
        $is_success = $user->changeImg($img_id, $is_checked, $pid);
        if ($is_success === false) {
            $this->error($user->getErrors());
        }
        $this->success('修改成功');
    }

    /**
     * 批量更改用户图片权限
    */
    public function actionBatchChangeImg() {
        $userid = Yii::app()->request->getParam('userid'); //用户
        $checked = Yii::app()->request->getParam('checked'); //选中还是取消
        $pid = Yii::app()->request->getParam('pid'); // 相册id
        if (empty($userid)) {
            $this->error('参数非法');
        }
        $user = User::model()->findByPk($userid);
        if ($user === null) {
            $this->error('用户不存在');
        }
        $is_success = $user->batchChangeImg($pid,$checked,$userid);
        if ($is_success === false) {
            $this->error($user->getErrors());
        }
        $this->success('修改成功');
    }

}